﻿#include "MaterialFloatNodeModel.h"

#include "MaterialGraph/Pin/MaterialGraphPinModel.h"
#include "Object/NewObject.h"

void RMaterialFloatNodeModel::FirstTimeInitializeObject()
{
    ParameterName = U"参数1";

    // Create the output pin
    auto OutputPin = NewObject<RMaterialGraphPinModel>(this);
    OutputPin->bIsOutput = true;
    OutputPin->ValueType = EMaterialGraphValueType::Vector1D;
    Outputs.Add(OutputPin);
}

CString RMaterialFloatNodeModel::GenerateExpression()
{
    
    CString Result=U"float "+Outputs[0]->UniqueOutputPinName+U" = ";

    if(bIsParameter)
    {
        int64_t IndexInArray=FloatParameterOffset/4;
        int64_t Component=FloatParameterOffset%4;
        
        Result+=U"FloatParameters.Floats["+CString(IndexInArray)+U"].";

        switch(Component)
        {
            case 0:
                Result+=U"x";
                break;
            case 1:
                Result+=U"y";
                break;
            case 2:
                Result+=U"z";
                break;
            case 3:
                Result+=U"w";
                break;
        }
    }
    else
    {
        Result+=CString(Value);
    }

    Result+=U";\n";
    return Result;
}

void RMaterialFloatNodeModel::SetIsParameter(bool InIsParameter)
{
    if(bIsParameter==InIsParameter)
    {
        return;
    }

    bIsParameter=InIsParameter;

    OnIsParameterChanged.ExecuteIfBound();
}

void RMaterialFloatNodeModel::CollectParameterAndTextures(TVector<float> &InOutFloatParameters, TVector<TSharedObjectPtr<RTexture2D>> &InOutTexture2Ds, TMap<CString, SMaterialParameterDescription> &InOutParameterDescriptions)
{
    if(!bIsParameter)
    {
        return;
    }

    SMaterialParameterDescription Description;
    Description.Type = EMaterialParameterType::Float;
    Description.Offset = InOutFloatParameters.Num();
    InOutFloatParameters.Add(Value);
    InOutParameterDescriptions.Add(ParameterName, Description);
}
